﻿@{
    ViewBag.Title = "AllContractInfoList";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<script type="text/javascript">
    $(function () {
        $("#ProjectID").combobox({
            editable: false, panelHeight: "auto",
            valueField: 'ID',
            textField: 'ProjectName',
            url: '/ProjectInfo/ProjectInfoListForFlow',
            onLoadSuccess: function () {
                var data = $("#ProjectID").combobox('getData');
                if (data.length > 0) {
                    $("#ProjectID").combobox('select', data[0]["ID"]);
                }
                else {
                    initTable('');
                }
            },
            onSelect: function (record) {
                initTable(record.ID);
            }
        });
    });

    function initTable(projectID) {
        $('#allContractInfoTreeGrid').treegrid({
            url: '/ContractInfo/GetAllContractInfoList?pid=' + projectID,
            nowrap: true,
            autoRowHeight: false,
            singleSelect: true,
            striped: true,
            pagination: false,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            treeField: 'ContractName',
            parentField: 'DisplayPID',
            toolbar: '#allContractInfoToolBar',
            columns: [[
                        { title: 'ID', field: 'ID', hidden: true },
                        { title: 'PID', field: 'PID', hidden: true },
                        { title: 'ContractCode', field: 'ContractCode', hidden: true },
                        { title: 'ContractSource', field: 'ContractSource', hidden: true },
                        { title: '合同名称', field: 'ContractName', width: 180 },
                        { title: '单位名称', field: 'ContractUnitName', width: 280 },
                        { title: '合同金额', field: 'ContractMoney', width: 150 },
                        { title: '施工图金额', field: 'ConstructionDrawMoney', width: 150 },
                        { title: '变更增减金额', field: 'AlterIncreaseOrDecreaseMoney', width: 150 },
                        { title: '变更后金额', field: 'AfterAlterMoney', width: 150 },
                        { title: '已计量金额', field: 'AccumulativeEstimateMoney', width: 150 },
                        { title: '计量百分比', field: 'AccumulativeEstimateProportion', width: 80 }
            ]],
            onClickRow: function (row) {
                $('#btnModify').linkbutton('enable');
                if (row.ContractSource == "施工类" || row.ContractSource == "") {
                    $('#btnDelete').linkbutton('disable');
                    if (row.ContractSource == "") {
                        $('#btnModify').linkbutton('disable');
                    }
                }
                else {
                    $('#btnDelete').linkbutton('enable');
                }
            },
            //双击弹出预览
            onDblClickRow: function (row) {
                AllContractList.Preview();
            }
        });
    }

    var AllContractList = {
        //新增
        Add: function () {
            //debugger;
            var data = $("#ProjectID").combobox('getData');
            if (data.length > 0) {
                var projectID = $("#ProjectID").combobox('getValue');
                AllContractList.Mutual("新增", "/ContractInfo/GetCommonContractInfo?PID=" + projectID, "/ContractInfo/SaveCommonContractInfo", 840, true, true);
            }
            else {
                $.EasyuiSlide("请先选择项目");
            }
        },

        //修改
        Modify: function () {
            var row = $("#allContractInfoTreeGrid").treegrid("getSelected");
            if (row) {
                if (row.ContractSource == "其他类") {
                    AllContractList.Mutual("修改", "/ContractInfo/GetCommonContractInfo?ID=" + row.ID, "/ContractInfo/SaveCommonContractInfo", 840, true, false, true, true);
                }
                else {
                    if (row.ContractCode != "*****合计*****") {
                        //ContractInfo.Mutual("修改", "/ContractInfo/GetContractInfo?ID=" + row.ID, "/ContractInfo/SaveContractInfo", 1050);
                        AllContractList.PreviewMutual("修改", "/ContractInfo/GetContractInfo?ID=" + row.ID, 1050, false, false, true, true);
                    }
                }
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //实体操作
        Mutual: function (title, href, saveUrl, width, isCommon, isAdd, maximizable, maximized) {
            $.ModelDialog({
                title: title, href: href, width: width, maximizable: maximizable, maximized: maximized,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#contractInfo-model",
                        url: saveUrl,
                        success: function (result) { AllContractList.Reload(); }
                    });
                },
                onLoad: function () { AllContractList.FormInit(isCommon, isAdd, false); }
            });
        },

        //弹窗初始化
        FormInit: function (isCommon, isAdd, isPreview) {
            if (isCommon) {
                if (isPreview) {
                    //$.MiniUEditorInit("ContractContent", 670, 105, true);  //迷你加载
                    $.MiniUEditorInit($("#hID").val(), "ContractManagement/AllContractList/ContractContent", "ContractContent", 670, 105, true);  //迷你加载
                }
                else {
                    //$.MiniUEditorInit("ContractContent", 670, 105, false);  //迷你加载
                    $.MiniUEditorInit($("#hID").val(), "ContractManagement/AllContractList/ContractContent", "ContractContent", 670, 105, false);  //迷你加载
                    $("#CorrespondenceUnit").combotree({
                        url: "/OrgInfo/GetOrgComboTree?emptyStr=",
                        onLoadSuccess: function () {
                            if (isAdd) {
                                var t = $("#CorrespondenceUnit").combotree('tree');
                                var data = t.tree('getRoots');
                                if (data.length > 0) {
                                    $("#CorrespondenceUnit").combotree('setValue', data[0].id);
                                }
                            }
                        }
                    });

                    AllContractList.FromDateTimeInit();

                    $('#OldStartDate').val(formatDateToHLine($('#ScheduledStartDate').datebox('getValue'), "/", "-"));
                    $('#OldEndDate').val(formatDateToHLine($('#ScheduledEndDate').datebox('getValue'), "/", "-"));
                }
            }
        },

        FromDateTimeInit: function () {
            $('#ScheduledStartDate').datebox({
                onSelect: function (date) {
                    var y = date.getFullYear();
                    var m = date.getMonth() + 1;
                    if (m < 10) {
                        m = "0" + m;
                    }
                    var d = date.getDate();
                    if (d < 10) {
                        d = "0" + d;
                    }
                    var startDate = y + '-' + m + '-' + d;

                    var oldStartDate = $('#OldStartDate').val();
                    var endDate = formatDateToHLine($('#ScheduledEndDate').datebox('getValue'), "/", "-");
                    if (endDate != '' && !compareDate(startDate, endDate, "-")) {
                        $('#ScheduledStartDate').datebox('setValue', oldStartDate);
                        $.EasyuiSlide("开始时间必须小于结束时间");
                    }
                    else {
                        $('#OldStartDate').val(startDate);
                        if (endDate != '') {
                            $('#ProjectPeriod').val(dateInterval(startDate, endDate, "-"));
                        }
                    }
                }
            });

            $('#ScheduledEndDate').datebox({
                onSelect: function (date) {
                    var y = date.getFullYear();
                    var m = date.getMonth() + 1;
                    if (m < 10) {
                        m = "0" + m;
                    }
                    var d = date.getDate();
                    if (d < 10) {
                        d = "0" + d;
                    }
                    var endDate = y + '-' + m + '-' + d;

                    var oldEndDate = $('#OldEndDate').val();
                    var startDate = formatDateToHLine($('#ScheduledStartDate').datebox('getValue'), "/", "-");
                    if (startDate != '' && !compareDate(startDate, endDate, "-")) {
                        $('#ScheduledEndDate').datebox('setValue', oldEndDate);
                        $.EasyuiSlide("开始时间必须小于结束时间");
                    }
                    else {
                        $('#OldEndDate').val(endDate);
                        if (startDate != '') {
                            $('#ProjectPeriod').val(dateInterval(startDate, endDate, "-"));
                        }
                    }
                }
            });
        },

        //预览
        Preview: function () {
            var row = $("#allContractInfoTreeGrid").treegrid("getSelected");
            if (row) {
                if (row.ContractSource == "其他类") {
                    AllContractList.PreviewMutual("预览", "/ContractInfo/PreviewCommonContractInfo?ID=" + row.ID, 840, true, true, true);
                }
                else {
                    if (row.ContractCode != "*****合计*****") {
                        AllContractList.PreviewMutual("预览", "/ContractInfo/PreviewContractInfo?ID=" + row.ID, 1050, false, true, true);
                    }
                }
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //实体操作
        PreviewMutual: function (title, href, width, isCommon, maximizable, maximized) {
            $.ModelDialog({
                title: title, href: href, width: width, maximizable: maximizable, maximized: maximized,
                onLoad: function () { AllContractList.FormInit(isCommon, false, true); }
            });
        },

        //删除
        Delete: function () {
            var arr = $('#allContractInfoTreeGrid').treegrid('getSelections');

            if (arr.length == 1 && arr[0].ContractCode != "*****合计*****") {
                $.messager.confirm('操作提示', "确认删除[ " + arr[0].ContractName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/ContractInfo/Delete?ID=" + arr[0].ID, { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                $.EasyuiSlide(result.Message);
                                AllContractList.Reload();
                            }
                            else {
                                $.EasyuiMsg(result.Message);
                            }
                        });
                    }
                });

            } else {
                $.EasyuiMsg("请选择一条数据");
            }
            return false;
        },

        //刷新
        Reload: function () {
            $("#allContractInfoTreeGrid").treegrid("reload");
        }
    }
</script>

<div class="easyui-layout" fit="true">
    <div data-options="region:'north'" style="height:35px;">
        <table style="margin-top: 5px">
            <tr>
                <td style="width: 95px; text-align: center">项目名称</td>
                <td>
                    <input id="ProjectID" name="ProjectID" class="easyui-combobox" style="width: 300px;" />
                </td>
            </tr>
        </table>
    </div>

    <div data-options="region:'center'">
        <table id="allContractInfoTreeGrid" fit="true"></table>
        @*<div id="allContractInfoToolBar">
                <a href='javascript:void(0)' class='easyui-linkbutton' onclick='AllContractList.Add()' data-options='plain:true,iconCls:"icon-application_add"'>新增</a>
                <a href='javascript:void(0)' id="btnModify" class='easyui-linkbutton' onclick='AllContractList.Modify()' data-options='plain:true,iconCls:"icon-application_delete"'>修改</a>
                <a href='javascript:void(0)' id="btnDelete" class='easyui-linkbutton' onclick='AllContractList.Delete()' data-options='plain:true,iconCls:"icon-bell"'>删除</a>
            </div>*@

        <div id="allContractInfoToolBar">
            @Html.Raw(ViewBag.ToolBarInfo)
        </div>
    </div>
</div>

